/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame;


import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.bullet.PhysicsTickListener;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.objects.PhysicsRigidBody;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

/**
 *
 * @author Peter
 */
public abstract class Tower extends AbstractAppState  {
    
    private int delay = 3;
    private PhysicsRigidBody targetedMinion;
    protected AttType attType;
     protected Main mainApp;
     protected Spatial tower1;
     protected SimpleApplication app;
     protected Spatial projectile;

     
    public void Tower(Vector3f coords, Float scale){
        app.getStateManager().attach(this);
        //super.initialize(app.getStateManager(), app);
        
        this.setEnabled(true);
        tower1 = mainApp.getAssetManager().loadModel("Models/veza/tower.j3o");
        
          tower1.setLocalTranslation(0.2f, 0.2f, 0.2f);
         tower1.scale(0.2f, 0.2f, 0.2f);
        mainApp.getRootNode().attachChild(tower1);
    }
    
    public enum AttType
    {
        ATTBASIC, ATTPOISON;
    }   
    
    private boolean hasToShoot(PhysicsRigidBody minion){
        targetedMinion = enemyInRange(minion);
        if (targetedMinion != null)
        {
            shootAtTarget(targetedMinion);
            return true;
        }
        else
        {
            return false;
        }
    }
    
    private void shootAtTarget(PhysicsRigidBody minion)
    {
        shoot(minion);
        new Timer( delay * 1000, taskPerformer).start(); 

    }
    
     ActionListener taskPerformer = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
          shoot(targetedMinion);
      }

  };
    
    protected void shoot(PhysicsRigidBody m)
    {       
        //if (attType.equals(AttType.ATTBASIC))
        
            projectile = mainApp.getAssetManager().loadModel("Models/veza/projectile.j3o");
        
            projectile.setLocalTranslation(m.getPhysicsLocation().add(1f,1f,1f));
            projectile.scale(0.2f, 0.2f, 0.2f);
            
            BoxCollisionShape boxShape = new BoxCollisionShape();
            RigidBodyControl myPhys = new RigidBodyControl(boxShape, 0.0f);
            projectile.addControl(myPhys);
            mainApp.getRootNode().attachChild(projectile);
        
            //myPhys.
            projectile.move(new Vector3f(20f, 200f, 0f));
    }
     
    public PhysicsRigidBody enemyInRange(PhysicsRigidBody minion)
    {
        return targetedMinion;
    }
    
    
 
    /*
        @Override
    public void initialize(AppStateManager stateManager, Application app) {
      super.initialize(stateManager, app); 
      this.app = (SimpleApplication)app;          // cast to a more specific class
 
      // init stuff that is independent of whether state is PAUSED or RUNNING
      //this.app.getRootNode().attachChild(getX()); // modify scene graph...
      //this.app.doSomething();                     // call custom methods...
   }
   */
}
